package com.gbits.app.finance.api;

import java.io.Serializable;
import java.util.Collection;

public class Page<T> implements Serializable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final int DEFAULT_PAGE_SIZE = 5;
	
	/** 每页显示大小 */
	private long pageSize;
	/** 当前页码 */
	private long currentPage;
	/** 总记录数 */
	private long total;
	/** 显示的数据 */
	private Collection<T> data;
	
	public Page(long currentPage, long pageSize, long total, Collection<T> data)
	{
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.total = total;
		this.data = data;
	}

	public Page()
	{
		this(1, DEFAULT_PAGE_SIZE);
	}

	public Page(long currentPage, long pageSize)
	{
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}

	public Page(long currentPage)
	{
		this(currentPage, DEFAULT_PAGE_SIZE);
	}

	public long getPageSize()
	{
		if(this.pageSize < 1)
		{
			return DEFAULT_PAGE_SIZE;
		}
		return pageSize;
	}

	public void setPageSize(long pageSize)
	{
		this.pageSize = pageSize;
	}

	public long getCurrentPage()
	{
		return currentPage;
	}

	public void setCurrentPage(long currentPage)
	{
		this.currentPage = currentPage;
	}

	public long getTotal()
	{
		return total;
	}

	public void setTotal(long total)
	{
		this.total = total;
	}

	public Collection<T> getData()
	{
		return data;
	}

	public void setData(Collection<T> data)
	{
		this.data = data;
	}
	
	public long getFirstPage()
	{
		return 1;
	}
	
	public long getLastPage()
	{
		if(this.getTotal() == 0)
		{
			return this.getFirstPage();
		}
		
		if(this.getTotal() % this.getPageSize() == 0)
		{
			return this.getTotal() / this.getPageSize();
		}
		else
		{
			return this.getTotal() / this.getPageSize() + 1;
		}
	}
	
	public boolean hasNext()
	{
		return this.getCurrentPage() < this.getLastPage();
	}
	
	public boolean hasPrev()
	{
		return this.getCurrentPage() > this.getFirstPage();
	}
	
	public static long getStart(long currentPage, long pageSize)
	{
		if(currentPage < 1)
		{
			return 0;
		}
		return (currentPage - 1) * pageSize;
	}
	
}
